package com.chart.web.controller.chart.sta;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.chart.chart.domian.ChartConfigs;
import com.chart.chart.domian.dto.ChartDateTto;
import com.chart.chart.domian.vo.ChartSmartVo;
import com.chart.chart.domian.vo.ChartXDataVo;
import com.chart.chart.service.IChartConfigsService;
import com.chart.common.core.controller.BaseController;
import com.chart.common.core.domain.AjaxResult;
import com.chart.common.utils.ChartDataUtils;
import com.chart.common.utils.ExcelDataUtils;
import com.chart.common.utils.ExcelUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 专家图
 *
 * @author chart
 */

@RestController
@RequestMapping("/expert/chart")
public class ExpertChartController extends BaseController {


    @Autowired
    private IChartConfigsService chartConfigsService;


    //四重奏
    @PostMapping("/anscombeQuartet/importData")
    public AjaxResult anscombeQuartet(@RequestBody ChartDateTto chartDateTto) throws Exception {
        String chartValue = chartDateTto.getChartValue();
        List<List<String>> lists = ChartDataUtils.extractDataFromList(chartDateTto.getDataList());

        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode chartValueJson = objectMapper.readTree(chartValue);

        // 获取dataset节点
        ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");
        // 更新dataset中的source节点
        dataset = ChartDataUtils.updateDatasetSources(dataset, lists, objectMapper);

        // 移除集合中第一个对象的第一个元素
        if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
            lists.get(0).remove(0);
        }

        // 获取series字段并删除其余对象
        ArrayNode seriesArray = (ArrayNode) chartValueJson.path("series");
        if (seriesArray.size() > 1) {
            while (seriesArray.size() > 1) {
                seriesArray.remove(1);
            }
        }

        // 计算第一个对象剩余元素数量
        int firstObjectRemainingElements = lists.isEmpty() ? 0 : lists.get(0).size();

        // 复制series的第一个对象
        JsonNode firstSeriesObject = seriesArray.get(0);
        for (int i = 1; i < firstObjectRemainingElements; i++) {
            seriesArray.add(firstSeriesObject.deepCopy());
        }

        // 在这里添加新的逻辑来处理 datasetIndex 字段
        for (int i = 0; i < seriesArray.size(); i++) {
            ObjectNode seriesObject = (ObjectNode) seriesArray.get(i);
            if (seriesObject.has("datasetIndex")) {
                seriesObject.put("datasetIndex", i);
            } else {
                seriesObject.put("datasetIndex", i);
            }
        }

        // 获取legend节点并更新data
        JsonNode legend = chartValueJson.path("legend");
        ChartDataUtils.updateLegendData(legend, lists);

        // 更新series中每个对象的name字段
        for (int i = 0; i < firstObjectRemainingElements; i++) {
            ((ObjectNode) seriesArray.get(i)).put("name", lists.get(0).get(i));
        }

        String updatedChartValue = chartValueJson.toString();

        ArrayList<ChartSmartVo> newList = new ArrayList<>();
        // 获取ChartConfigs对象
        ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
        // 创建新的ChartSmartVo对象并设置属性
        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigId(configs.getId()); // 假设ChartConfigs有getId方法
        chartSmartVo.setConfigName(configs.getConfigName()); // 假设ChartConfigs有getName方法
        chartSmartVo.setConfigValue(updatedChartValue);
        chartSmartVo.setChartTypeId(configs.getChartTypeId());

        newList.add(chartSmartVo);
        System.out.println(updatedChartValue);
        return AjaxResult.success(newList);

    }


    //带有几个变量的点曲线图
    @PostMapping("/pCWithSV/importData")
    public AjaxResult pCWithSV(@RequestBody ChartDateTto chartDateTto) throws Exception {
        String chartValue = chartDateTto.getChartValue();
        List<List<String>> lists = ChartDataUtils.extractDataFromList(chartDateTto.getDataList());

        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode chartValueJson = objectMapper.readTree(chartValue);

        // 获取dataset节点
        ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");
        // 更新dataset中的source节点
        dataset = ChartDataUtils.updateDatasetSources(dataset, lists, objectMapper);

        // 移除集合中第一个对象的第一个元素
        if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
            lists.get(0).remove(0);
        }

        // 获取series字段并删除其余对象
        ArrayNode seriesArray = (ArrayNode) chartValueJson.path("series");
        if (seriesArray.size() > 1) {
            while (seriesArray.size() > 1) {
                seriesArray.remove(1);
            }
        }

        // 计算第一个对象剩余元素数量
        int firstObjectRemainingElements = lists.isEmpty() ? 0 : lists.get(0).size();

        // 复制series的第一个对象
        JsonNode firstSeriesObject = seriesArray.get(0);
        for (int i = 1; i < firstObjectRemainingElements; i++) {
            seriesArray.add(firstSeriesObject.deepCopy());
        }

        // 在这里添加新的逻辑来处理 datasetIndex 字段
        for (int i = 0; i < seriesArray.size(); i++) {
            ObjectNode seriesObject = (ObjectNode) seriesArray.get(i);
            if (seriesObject.has("datasetIndex")) {
                seriesObject.put("datasetIndex", i);
            } else {
                seriesObject.put("datasetIndex", i);
            }
        }

        // 获取legend节点并更新data
        JsonNode legend = chartValueJson.path("legend");
        ChartDataUtils.updateLegendData(legend, lists);

        // 更新series中每个对象的name字段
        for (int i = 0; i < firstObjectRemainingElements; i++) {
            ((ObjectNode) seriesArray.get(i)).put("name", lists.get(0).get(i));
        }

        String updatedChartValue = chartValueJson.toString();

        ArrayList<ChartSmartVo> newList = new ArrayList<>();
        // 获取ChartConfigs对象
        ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
        // 创建新的ChartSmartVo对象并设置属性
        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigId(configs.getId()); // 假设ChartConfigs有getId方法
        chartSmartVo.setConfigName(configs.getConfigName()); // 假设ChartConfigs有getName方法
        chartSmartVo.setConfigValue(updatedChartValue);
        chartSmartVo.setChartTypeId(configs.getChartTypeId());

        newList.add(chartSmartVo);
        System.out.println(updatedChartValue);
        return AjaxResult.success(newList);
    }



    //成对分类图
    @PostMapping("/pairedClass/importData")
    public AjaxResult pairedClass(@RequestBody ChartDateTto chartDateTto) throws Exception {
        String chartValue = chartDateTto.getChartValue();
        List<List<String>> lists = ChartDataUtils.extractDataFromList(chartDateTto.getDataList());

        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode chartValueJson = objectMapper.readTree(chartValue);

        // 获取dataset节点
        ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");
        // 更新dataset中的source节点
        dataset = ChartDataUtils.updateDatasetSources(dataset, lists, objectMapper);

        // 移除集合中第一个对象的第一个元素
        if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
            lists.get(0).remove(0);
        }

        // 获取series字段并删除其余对象
        ArrayNode seriesArray = (ArrayNode) chartValueJson.path("series");
        if (seriesArray.size() > 1) {
            while (seriesArray.size() > 1) {
                seriesArray.remove(1);
            }
        }

        // 计算第一个对象剩余元素数量
        int firstObjectRemainingElements = lists.isEmpty() ? 0 : lists.get(0).size();

        // 复制series的第一个对象
        JsonNode firstSeriesObject = seriesArray.get(0);
        for (int i = 1; i < firstObjectRemainingElements; i++) {
            seriesArray.add(firstSeriesObject.deepCopy());
        }

        // 在这里添加新的逻辑来处理 datasetIndex 字段
        for (int i = 0; i < seriesArray.size(); i++) {
            ObjectNode seriesObject = (ObjectNode) seriesArray.get(i);
            if (seriesObject.has("datasetIndex")) {
                seriesObject.put("datasetIndex", i);
            } else {
                seriesObject.put("datasetIndex", i);
            }
        }

        // 获取legend节点并更新data
        JsonNode legend = chartValueJson.path("legend");
        ChartDataUtils.updateLegendData(legend, lists);

        // 更新series中每个对象的name字段
        for (int i = 0; i < firstObjectRemainingElements; i++) {
            ((ObjectNode) seriesArray.get(i)).put("name", lists.get(0).get(i));
        }

        String updatedChartValue = chartValueJson.toString();

        ArrayList<ChartSmartVo> newList = new ArrayList<>();
        // 获取ChartConfigs对象
        ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
        // 创建新的ChartSmartVo对象并设置属性
        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigId(configs.getId()); // 假设ChartConfigs有getId方法
        chartSmartVo.setConfigName(configs.getConfigName()); // 假设ChartConfigs有getName方法
        chartSmartVo.setConfigValue(updatedChartValue);
        chartSmartVo.setChartTypeId(configs.getChartTypeId());

        newList.add(chartSmartVo);
        System.out.println(updatedChartValue);
        return AjaxResult.success(newList);
    }

    //多面多层圆滑折线图、成对多层圆滑折线图
    //multiSidAndLaySmooth/importData
    @PostMapping("/multiSidAndLaySmooth/importData")
    public AjaxResult multiSidAndLaySmooth(@RequestBody ChartDateTto chartDateTto) throws Exception {
        String chartValue = chartDateTto.getChartValue();
        List<List<String>> lists = ChartDataUtils.extractDataFromList(chartDateTto.getDataList());

        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode chartValueJson = objectMapper.readTree(chartValue);

        // 获取dataset节点
        ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");
        // 更新dataset中的source节点
        dataset = ChartDataUtils.updateDatasetSources(dataset, lists, objectMapper);

        // 移除集合中第一个对象的第一个元素
        if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
            lists.get(0).remove(0);
        }

        // 获取series字段并删除其余对象
        ArrayNode seriesArray = (ArrayNode) chartValueJson.path("series");
        if (seriesArray.size() > 1) {
            while (seriesArray.size() > 1) {
                seriesArray.remove(1);
            }
        }

        // 计算第一个对象剩余元素数量
        int firstObjectRemainingElements = lists.isEmpty() ? 0 : lists.get(0).size();

        // 复制series的第一个对象
        JsonNode firstSeriesObject = seriesArray.get(0);
        for (int i = 1; i < firstObjectRemainingElements; i++) {
            seriesArray.add(firstSeriesObject.deepCopy());
        }

        // 在这里添加新的逻辑来处理 datasetIndex 字段
        for (int i = 0; i < seriesArray.size(); i++) {
            ObjectNode seriesObject = (ObjectNode) seriesArray.get(i);
            if (seriesObject.has("datasetIndex")) {
                seriesObject.put("datasetIndex", i);
            } else {
                seriesObject.put("datasetIndex", i);
            }
        }

        // 获取legend节点并更新data
        JsonNode legend = chartValueJson.path("legend");
        ChartDataUtils.updateLegendData(legend, lists);

        // 更新series中每个对象的name字段
        for (int i = 0; i < firstObjectRemainingElements; i++) {
            ((ObjectNode) seriesArray.get(i)).put("name", lists.get(0).get(i));
        }

        String updatedChartValue = chartValueJson.toString();

        ArrayList<ChartSmartVo> newList = new ArrayList<>();
        // 获取ChartConfigs对象
        ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
        // 创建新的ChartSmartVo对象并设置属性
        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigId(configs.getId()); // 假设ChartConfigs有getId方法
        chartSmartVo.setConfigName(configs.getConfigName()); // 假设ChartConfigs有getName方法
        chartSmartVo.setConfigValue(updatedChartValue);
        chartSmartVo.setChartTypeId(configs.getChartTypeId());

        newList.add(chartSmartVo);
        System.out.println(updatedChartValue);
        return AjaxResult.success(newList);

    }    //散点图矩阵
    @PostMapping("/scatterplotMatrix/importData")
    public AjaxResult scatterplotMatrix(@RequestBody ChartDateTto chartDateTto) throws Exception {
        String chartValue = chartDateTto.getChartValue();
        List<List<String>> lists = ChartDataUtils.extractDataFromList(chartDateTto.getDataList());

        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode chartValueJson = objectMapper.readTree(chartValue);

        // 获取dataset节点
        ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");
        // 更新dataset中的source节点
        dataset = ChartDataUtils.updateDatasetSources(dataset, lists, objectMapper);

        // 移除集合中第一个对象的第一个元素
        if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
            lists.get(0).remove(0);
        }

        // 获取series字段并删除其余对象
        ArrayNode seriesArray = (ArrayNode) chartValueJson.path("series");
        if (seriesArray.size() > 1) {
            while (seriesArray.size() > 1) {
                seriesArray.remove(1);
            }
        }

        // 计算第一个对象剩余元素数量
        int firstObjectRemainingElements = lists.isEmpty() ? 0 : lists.get(0).size();

        // 复制series的第一个对象
        JsonNode firstSeriesObject = seriesArray.get(0);
        for (int i = 1; i < firstObjectRemainingElements; i++) {
            seriesArray.add(firstSeriesObject.deepCopy());
        }

        // 在这里添加新的逻辑来处理 datasetIndex 字段
        for (int i = 0; i < seriesArray.size(); i++) {
            ObjectNode seriesObject = (ObjectNode) seriesArray.get(i);
            if (seriesObject.has("datasetIndex")) {
                seriesObject.put("datasetIndex", i);
            } else {
                seriesObject.put("datasetIndex", i);
            }
        }

        // 获取legend节点并更新data
        JsonNode legend = chartValueJson.path("legend");
        ChartDataUtils.updateLegendData(legend, lists);

        // 更新series中每个对象的name字段
        for (int i = 0; i < firstObjectRemainingElements; i++) {
            ((ObjectNode) seriesArray.get(i)).put("name", lists.get(0).get(i));
        }

        String updatedChartValue = chartValueJson.toString();

        ArrayList<ChartSmartVo> newList = new ArrayList<>();
        // 获取ChartConfigs对象
        ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
        // 创建新的ChartSmartVo对象并设置属性
        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigId(configs.getId()); // 假设ChartConfigs有getId方法
        chartSmartVo.setConfigName(configs.getConfigName()); // 假设ChartConfigs有getName方法
        chartSmartVo.setConfigValue(updatedChartValue);
        chartSmartVo.setChartTypeId(configs.getChartTypeId());

        newList.add(chartSmartVo);
        System.out.println(updatedChartValue);
        return AjaxResult.success(newList);
    }

/*

    //四重奏
    @PostMapping("/anscombeQuartet/importData")
    public AjaxResult anscombeQuartet(MultipartFile file, Long configId) throws Exception {

        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            getdata.remove(0);
            int dataSize = getdata.size();

            int dataIndex = dataSize / 4;

            List<String> maths = new ArrayList<>();

            List<ChartXDataVo> chartXDataVos = new ArrayList<>();


            List<List<Object>> doubles = new ArrayList<>();


            for (int i = 0; i < dataSize; i++) {
                if (i == 0) {
//                    maths.add(getdata.get(i).get(1));
                } else if (i == dataIndex || i == 2 * dataIndex || i == 3 * dataIndex) {
//                    maths.add(getdata.get(i).get(1));
                    ChartXDataVo xDataVo = new ChartXDataVo();
                    xDataVo.setSource(doubles);
                    chartXDataVos.add(xDataVo);
                    doubles = new ArrayList<>();
                } else {
                    List<Object> list = new ArrayList<>();
                    list.add(Double.parseDouble(getdata.get(i).get(0)));
                    list.add(Double.parseDouble(getdata.get(i).get(1)));
                    doubles.add(list);
                    if (i == dataSize - 1) {
                        ChartXDataVo xDataVo = new ChartXDataVo();
                        xDataVo.setSource(doubles);
                        chartXDataVos.add(xDataVo);
                    }
                }
            }


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            jsonObject.put("dataset", chartXDataVos);

            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");

            ArrayList<JSONObject> jsonObjects = new ArrayList<>();
            for (int i = 0; i < chartXDataVos.size(); i++) {
                ChartXDataVo chartXDataVo = chartXDataVos.get(i);
                List<List<Object>> lists = chartXDataVo.getSource();
                double[] x = new double[lists.size()];
                double[] y = new double[lists.size()];

                for (int i1 = 0; i1 < lists.size(); i1++) {
                    List<Object> sources = lists.get(i1);
                    for (int i2 = 0; i2 < sources.size(); i2++) {
                        x[i1] = Double.parseDouble(sources.get(0).toString());
                        y[i1] = Double.parseDouble(sources.get(1).toString());
                    }
                }
                //计算出线性回归方程   &&找出最大最小值点对应方程的值
                Map service = ExcelDataUtils.getService1(x, y);

                JSONObject jsonObject1 = series.get(i);
                JSONObject markLine = (JSONObject) jsonObject1.get("markLine");
                JSONObject tooltip = (JSONObject) markLine.get("tooltip");
                tooltip.put("formatter", service.get("formatter"));
                List<JSONObject> data = (List<JSONObject>) markLine.get("data");
                for (int i1 = 0; i1 < data.size(); i1++) {
                    List<JSONObject> jsonObject2s = (List<JSONObject>) data.get(i1);
                    for (int i2 = 0; i2 < jsonObject2s.size(); i2++) {
                        JSONObject jsonObject2 = jsonObject2s.get(i2);
                        if (i2 == 0) {
                            jsonObject2.put("coord", service.get("min"));
                        } else {
                            jsonObject2.put("coord", service.get("max"));
                        }
                    }
                }
                markLine.put("tooltip", tooltip);
                markLine.put("data", data);
                jsonObject1.put("markLine", markLine);
                jsonObjects.add(jsonObject1);
            }
            jsonObject.put("series", jsonObjects);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        } finally {
            workbook.close();
        }
    }
*/


    //四重奏
    @PostMapping("/anscombeQuartet/v2/importData")
    public AjaxResult v2anscombeQuartet(@RequestBody List<Map<String, Object>> dataList, Long configId) throws Exception {


        //XSSFWorkbook workbook = null;
        try {
         /*   workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);*/
            Sheet sheet = ExcelUtils.convertDataListToSheets(dataList);

            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            getdata.remove(0);
            int dataSize = getdata.size();

            int dataIndex = dataSize / 4;

            List<String> maths = new ArrayList<>();

            List<ChartXDataVo> chartXDataVos = new ArrayList<>();


            List<List<Object>> doubles = new ArrayList<>();


            for (int i = 0; i < dataSize; i++) {
                if (i == 0) {
//                    maths.add(getdata.get(i).get(1));
                } else if (i == dataIndex || i == 2 * dataIndex || i == 3 * dataIndex) {
//                    maths.add(getdata.get(i).get(1));
                    ChartXDataVo xDataVo = new ChartXDataVo();
                    xDataVo.setSource(doubles);
                    chartXDataVos.add(xDataVo);
                    doubles = new ArrayList<>();
                } else {
                    List<Object> list = new ArrayList<>();
                    list.add(Double.parseDouble(getdata.get(i).get(0)));
                    list.add(Double.parseDouble(getdata.get(i).get(1)));
                    doubles.add(list);
                    if (i == dataSize - 1) {
                        ChartXDataVo xDataVo = new ChartXDataVo();
                        xDataVo.setSource(doubles);
                        chartXDataVos.add(xDataVo);
                    }
                }
            }


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            jsonObject.put("dataset", chartXDataVos);

            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");

            ArrayList<JSONObject> jsonObjects = new ArrayList<>();
            for (int i = 0; i < chartXDataVos.size(); i++) {
                ChartXDataVo chartXDataVo = chartXDataVos.get(i);
                List<List<Object>> lists = chartXDataVo.getSource();
                double[] x = new double[lists.size()];
                double[] y = new double[lists.size()];

                for (int i1 = 0; i1 < lists.size(); i1++) {
                    List<Object> sources = lists.get(i1);
                    for (int i2 = 0; i2 < sources.size(); i2++) {
                        x[i1] = Double.parseDouble(sources.get(0).toString());
                        y[i1] = Double.parseDouble(sources.get(1).toString());
                    }
                }
                //计算出线性回归方程   &&找出最大最小值点对应方程的值
                Map service = ExcelDataUtils.getService1(x, y);

                JSONObject jsonObject1 = series.get(i);
                JSONObject markLine = (JSONObject) jsonObject1.get("markLine");
                JSONObject tooltip = (JSONObject) markLine.get("tooltip");
                tooltip.put("formatter", service.get("formatter"));
                List<JSONObject> data = (List<JSONObject>) markLine.get("data");
                for (int i1 = 0; i1 < data.size(); i1++) {
                    List<JSONObject> jsonObject2s = (List<JSONObject>) data.get(i1);
                    for (int i2 = 0; i2 < jsonObject2s.size(); i2++) {
                        JSONObject jsonObject2 = jsonObject2s.get(i2);
                        if (i2 == 0) {
                            jsonObject2.put("coord", service.get("min"));
                        } else {
                            jsonObject2.put("coord", service.get("max"));
                        }
                    }
                }
                markLine.put("tooltip", tooltip);
                markLine.put("data", data);
                jsonObject1.put("markLine", markLine);
                jsonObjects.add(jsonObject1);
            }
            jsonObject.put("series", jsonObjects);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        } finally {
           // workbook.close();
        }
    }

/*
    //带有几个变量的点曲线图
    @PostMapping("/pCWithSV/importData")
    public AjaxResult pCWithSV(MultipartFile file, Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getColData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> groupName = new ArrayList<>();


            for (int i = 1; i < getdata.size(); i++) {
                List<String> ssname = getdata.get(i);
                String gname = ssname.get(0);
                if (ObjectUtils.isNotEmpty(gname) && !gname.equals("")) {
                    groupName.add(gname);
                }
            }

            List<String> xData = new ArrayList<>();
            List<String> list1 = getdata.get(0);
            for (int i = 1; i < list1.size(); i++) {
                String s = list1.get(i);
                if (ObjectUtils.isNotEmpty(s) && !s.equals("")) {
                    xData.add(s);
                }
            }


            List<Map> newMaps = new ArrayList<>();

            for (int i = 0; i < groupName.size(); i++) {
                List<String> data = getdata.get(i + 1);
                List<List<String>> newList = new ArrayList<>();
                for (int i1 = 0; i1 < xData.size(); i1++) {
                    String s = data.get(i1);
                    if (ObjectUtils.isNotEmpty(s) && !s.equals("")) {
                        List<String> list = new ArrayList<>();
                        list.add(xData.get(i1));
                        list.add(s);
                        newList.add(list);
                    }
                }
                Map<String, List<List<String>>> map = new HashMap<>();
                map.put("source", newList);
                newMaps.add(map);
            }

            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");

            ArrayList<JSONObject> jsonObjects = new ArrayList<>();

            for (int i = 0; i < series.size(); i++) {
                JSONObject jsonObject1 = series.get(i);
                jsonObject1.put("name", groupName.get(i));
                jsonObjects.add(jsonObject1);
            }


            jsonObject.put("series", jsonObjects);

            jsonObject.put("dataset", newMaps);

            JSONObject legend = (JSONObject) jsonObject.get("legend");
            List<JSONObject> data = (List<JSONObject>) legend.get("data");
            for (int i = 0; i < data.size(); i++) {
                JSONObject jsonObject1 = data.get(i);
                jsonObject1.put("name", groupName.get(i));
            }
            legend.put("data", data);
            jsonObject.put("legend", legend);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        } finally {
            workbook.close();
        }
    }*/


    //带有几个变量的点曲线图
    @PostMapping("/pCWithSV/v2/importData")
    public AjaxResult v2pCWithSV(@RequestBody List<Map<String, Object>> dataList, Long configId) throws Exception {
      //  XSSFWorkbook workbook = null;
        try {
       /*     workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);*/

            Sheet sheet = ExcelUtils.convertDataListToSheetRemoveNineAndOne(dataList);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getColData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> groupName = new ArrayList<>();


            for (int i = 1; i < getdata.size(); i++) {
                List<String> ssname = getdata.get(i);
                String gname = ssname.get(0);
                if (ObjectUtils.isNotEmpty(gname) && !gname.equals("")) {
                    groupName.add(gname);
                }
            }

            List<String> xData = new ArrayList<>();
            List<String> list1 = getdata.get(0);
            for (int i = 1; i < list1.size(); i++) {
                String s = list1.get(i);
                if (ObjectUtils.isNotEmpty(s) && !s.equals("")) {
                    xData.add(s);
                }
            }


            List<Map> newMaps = new ArrayList<>();

            for (int i = 0; i < groupName.size(); i++) {
                List<String> data = getdata.get(i + 1);
                List<List<String>> newList = new ArrayList<>();
                for (int i1 = 0; i1 < xData.size(); i1++) {
                    String s = data.get(i1);
                    if (ObjectUtils.isNotEmpty(s) && !s.equals("")) {
                        List<String> list = new ArrayList<>();
                        list.add(xData.get(i1));
                        list.add(s);
                        newList.add(list);
                    }
                }
                Map<String, List<List<String>>> map = new HashMap<>();
                map.put("source", newList);
                newMaps.add(map);
            }

            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");

            ArrayList<JSONObject> jsonObjects = new ArrayList<>();

            for (int i = 0; i < series.size(); i++) {
                JSONObject jsonObject1 = series.get(i);
                jsonObject1.put("name", groupName.get(i));
                jsonObjects.add(jsonObject1);
            }


            jsonObject.put("series", jsonObjects);

            jsonObject.put("dataset", newMaps);

            JSONObject legend = (JSONObject) jsonObject.get("legend");
            List<JSONObject> data = (List<JSONObject>) legend.get("data");
            for (int i = 0; i < data.size(); i++) {
                JSONObject jsonObject1 = data.get(i);
                jsonObject1.put("name", groupName.get(i));
            }
            legend.put("data", data);
            jsonObject.put("legend", legend);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        } finally {
          //  workbook.close();
        }
    }
/*    //成对分类图
    @PostMapping("/pairedClass/importData")
    public AjaxResult pairedClass(MultipartFile file, Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> groupName = new ArrayList<>();

            List<List<List<Object>>> newList = new ArrayList<>();

            List<List<Object>> list = new ArrayList<>();


            for (int i = 1; i < getdata.size(); i++) {
                List<String> sdata = getdata.get(i);
                List<Object> newList1 = new ArrayList<>();
                for (int i1 = 0; i1 < sdata.size(); i1++) {
                    String s = sdata.get(i1);
                    if (ObjectUtils.isNotEmpty(s) && !s.equals("")) {
                        if (i1 == 1) {
                            newList1.add(Double.parseDouble(s));
                        } else {
                            newList1.add(s);
                        }
                    }
                }


                if (i == getdata.size() - 1) {
                    list.add(newList1);
                    newList.add(list);
                } else if (i == 1) {
                    List<String> gName = getdata.get(i);
                    groupName.add(gName.get(0));
                } else {
                    if (getdata.get(i).size() == 1) {
                        List<String> gName = getdata.get(i);
                        groupName.add(gName.get(0));
                        newList.add(list);
                        list = new ArrayList<>();
                    } else {
                        list.add(newList1);
                    }
                }
            }

            ArrayList<Map> maps = new ArrayList<>();

            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());


            for (int i = 0; i < newList.size(); i++) {
                List<List<Object>> lists = newList.get(i);
                HashMap<String, Object> map = new HashMap<>();
                map.put("source", lists);
                maps.add(map);
            }

            jsonObject.put("dataset", maps);
            return AjaxResult.success(JSON.toJSONString(jsonObject));
        } finally {
            workbook.close();
        }
    }*/




    //成对分类图
    @PostMapping("/pairedClass/v2/importData")
    public AjaxResult v2pairedClass(@RequestBody List<Map<String, Object>> dataList, Long configId) throws Exception {
       // XSSFWorkbook workbook = null;
        try {
   /*         workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);*/

            Sheet sheet = ExcelUtils.convertDataListToSheetRemoveNineAndOne(dataList);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> groupName = new ArrayList<>();

            List<List<List<Object>>> newList = new ArrayList<>();

            List<List<Object>> list = new ArrayList<>();


            for (int i = 1; i < getdata.size(); i++) {
                List<String> sdata = getdata.get(i);
                List<Object> newList1 = new ArrayList<>();
                for (int i1 = 0; i1 < sdata.size(); i1++) {
                    String s = sdata.get(i1);
                    if (ObjectUtils.isNotEmpty(s) && !s.equals("")) {
                        if (i1 == 1) {
                            newList1.add(Double.parseDouble(s));
                        } else {
                            newList1.add(s);
                        }
                    }
                }


                if (i == getdata.size() - 1) {
                    list.add(newList1);
                    newList.add(list);
                } else if (i == 1) {
                    List<String> gName = getdata.get(i);
                    groupName.add(gName.get(0));
                } else {
                    if (getdata.get(i).size() == 1) {
                        List<String> gName = getdata.get(i);
                        groupName.add(gName.get(0));
                        newList.add(list);
                        list = new ArrayList<>();
                    } else {
                        list.add(newList1);
                    }
                }
            }

            ArrayList<Map> maps = new ArrayList<>();

            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());


            for (int i = 0; i < newList.size(); i++) {
                List<List<Object>> lists = newList.get(i);
                HashMap<String, Object> map = new HashMap<>();
                map.put("source", lists);
                maps.add(map);
            }

            jsonObject.put("dataset", maps);
            return AjaxResult.success(JSON.toJSONString(jsonObject));
        } finally {
         //   workbook.close();
        }
    }
    //按数据子集绘制直方图
    @PostMapping("/subsetHistogram/importData")
    public AjaxResult subsetHistogram(MultipartFile file, Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> groupName = new ArrayList<>();

            List<List<List<Object>>> newList = new ArrayList<>();

            List<List<Object>> list = new ArrayList<>();


            for (int i = 1; i < getdata.size(); i++) {
                List<String> sdata = getdata.get(i);
                List<Object> newList1 = new ArrayList<>();
                for (int i1 = 0; i1 < sdata.size(); i1++) {
                    String s = sdata.get(i1);
                    if (ObjectUtils.isNotEmpty(s) && !s.equals("")) {
                        if (i1 == 1) {
                            newList1.add(Double.parseDouble(s));
                        } else {
                            newList1.add(s);
                        }
                    }
                }


                if (i == getdata.size() - 1) {
                    list.add(newList1);
                    newList.add(list);
                } else if (i == 1) {
                    List<String> gName = getdata.get(i);
                    groupName.add(gName.get(0));
                } else {
                    if (getdata.get(i).size() == 1) {
                        List<String> gName = getdata.get(i);
                        groupName.add(gName.get(0));
                        newList.add(list);
                        list = new ArrayList<>();
                    } else {
                        list.add(newList1);
                    }
                }
            }

            ArrayList<Map> maps = new ArrayList<>();

            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());


            for (int i = 0; i < newList.size(); i++) {
                List<List<Object>> lists = newList.get(i);
                HashMap<String, Object> map = new HashMap<>();
                map.put("source", lists);
                maps.add(map);
            }

            List<JSONObject> jsonObjects = new ArrayList<>();
            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject jsonObject1 = series.get(0);
            for (int i = 0; i < groupName.size(); i++) {
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.putAll(jsonObject1);
                jsonObject2.put("name", groupName.get(i));
                jsonObject2.put("xAxisIndex", i);
                jsonObject2.put("yAxisIndex", i);
                jsonObject2.put("datasetIndex", i);

                jsonObjects.add(jsonObject2);
            }

            jsonObject.put("dataset", maps);
            jsonObject.put("series", jsonObjects);
            return AjaxResult.success(JSON.toJSONString(jsonObject));
        } finally {
            workbook.close();
        }
    }

    //按数据子集绘制直方图
    @PostMapping("/subsetHistogram/v2/importData")
    public AjaxResult v2subsetHistogram(@RequestBody List<Map<String, Object>> dataList, Long configId) throws Exception {
      //  XSSFWorkbook workbook = null;
        try {
/*            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);*/

            Sheet sheet = ExcelUtils.convertDataListToSheetRemoveNineAndOne(dataList);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> groupName = new ArrayList<>();

            List<List<List<Object>>> newList = new ArrayList<>();

            List<List<Object>> list = new ArrayList<>();


            for (int i = 1; i < getdata.size(); i++) {
                List<String> sdata = getdata.get(i);
                List<Object> newList1 = new ArrayList<>();
                for (int i1 = 0; i1 < sdata.size(); i1++) {
                    String s = sdata.get(i1);
                    if (ObjectUtils.isNotEmpty(s) && !s.equals("")) {
                        if (i1 == 1) {
                            newList1.add(Double.parseDouble(s));
                        } else {
                            newList1.add(s);
                        }
                    }
                }


                if (i == getdata.size() - 1) {
                    list.add(newList1);
                    newList.add(list);
                } else if (i == 1) {
                    List<String> gName = getdata.get(i);
                    groupName.add(gName.get(0));
                } else {
                    if (getdata.get(i).size() == 1) {
                        List<String> gName = getdata.get(i);
                        groupName.add(gName.get(0));
                        newList.add(list);
                        list = new ArrayList<>();
                    } else {
                        list.add(newList1);
                    }
                }
            }

            ArrayList<Map> maps = new ArrayList<>();

            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());


            for (int i = 0; i < newList.size(); i++) {
                List<List<Object>> lists = newList.get(i);
                HashMap<String, Object> map = new HashMap<>();
                map.put("source", lists);
                maps.add(map);
            }

            List<JSONObject> jsonObjects = new ArrayList<>();
            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject jsonObject1 = series.get(0);
            for (int i = 0; i < groupName.size(); i++) {
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.putAll(jsonObject1);
                jsonObject2.put("name", groupName.get(i));
                jsonObject2.put("xAxisIndex", i);
                jsonObject2.put("yAxisIndex", i);
                jsonObject2.put("datasetIndex", i);

                jsonObjects.add(jsonObject2);
            }

            jsonObject.put("dataset", maps);
            jsonObject.put("series", jsonObjects);
            return AjaxResult.success(JSON.toJSONString(jsonObject));
        } finally {
         //   workbook.close();
        }
    }

/*
    //多面多层圆滑折线图、成对多层圆滑折线图
    //multiSidAndLaySmooth/importData
    @PostMapping("/multiSidAndLaySmooth/importData")
    public AjaxResult multiSidAndLaySmooth(MultipartFile file, Long configId) throws Exception {

        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取数据
            List<List<String>> excelData = ExcelDataUtils.getData(sheet);
            //数据判空
            if (excelData.size() <= 1) {
                return AjaxResult.error("请填充数据", null);
            }

            //这里就是表面分组
            List<Object> xilieList = new ArrayList<>();
            //获取分组
            List<String> xilis = excelData.get(0);
            for (int i = 0; i < xilis.size(); i++) {
                String s = xilis.get(i);
                if (ObjectUtils.isNotEmpty(s) && !s.equals("")) {
                    xilieList.add(s);
                }
            }

            //获取分组
            List<String> groupName = excelData.get(0).stream().skip(1).collect(Collectors.toList());
            excelData.remove(0);
            //封装返回数据
            //所有数据
            List<String> allData = excelData.stream().skip(1).flatMap(row -> row.stream().skip(1)).collect(Collectors.toList());
            //对数据进行字符串处理:获取最大最小值
            ExcelDataUtils.Result minMax = ExcelDataUtils.findMinMax(allData);

            //数据处理
            List<List<Object>> tempList = new ArrayList<>();
            List<List<List<Object>>> groupList = new ArrayList<>();
            int start = 0;
            int end = 0;
            for (int i = 0; i < excelData.size(); i++) {
                if (i < excelData.size() - 1 && excelData.get(i + 1).size() != 1) {
                    //0 - 24
                    //碰到end
                    end = i + 1;
                } else {
                    // i =25 end = 24
                    //遇到第一个size=1 将上一组数据归档
                    //开始操作
                    for (int j = start; j <= end; j++) {
                        List<Object> numbers = new ArrayList<>();
                        for (String s : excelData.get(j)) {
                            // 数据处理：如果是纯数字则转换为 Double，否则保持不变
                            if (s.matches("-?\\d+(\\.\\d+)?")) {
                                numbers.add(Double.parseDouble(s));
                            } else {
                                numbers.add(s);
                            }
                        }
                        tempList.add(numbers);
                    }
                    groupList.add(tempList);
                    tempList = new ArrayList<>();
                    start = end;
                }
            }
            //组装source
            List<List<List<Object>>> result = groupList.stream()
                    .skip(1) // 跳过第一个大集合
                    .map(innerList -> innerList.stream()
                            .skip(1) // 跳过每个内部集合的第一个元素
                            .collect(Collectors.toList()))
                    .collect(Collectors.toList());
            List<List<List<Object>>> groupData = new ArrayList<>();
            groupData.add(groupList.get(0));
            groupData.addAll(result);
            //source资源集合
            List<Map> maps = new ArrayList<>();

            for (List<List<Object>> item : groupData) {
                List<Object> title = item.get(0);
                title.addAll(groupName);
                item.remove(0);
                item.add(0, title);
                HashMap<String, Object> map = new HashMap<>();
                map.put("source", item);
                maps.add(map);
            }

            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            //组装json
            List<JSONObject> jsonObjects = new ArrayList<>();
            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject jsonObject1 = series.get(0);
            for (int i = 0; i < 2; i++) {
                for (int j = 1; j < xilieList.size(); j++) {
                    JSONObject jsonObject2 = new JSONObject();
                    jsonObject2.putAll(jsonObject1);
                    jsonObject2.put("name", xilieList.get(j));
                    jsonObject2.put("xAxisIndex", i);
                    jsonObject2.put("yAxisIndex", i);
                    jsonObject2.put("datasetIndex", i);
                    jsonObjects.add(jsonObject2);
                }
            }
            jsonObject.put("dataset", maps);
            jsonObject.put("series", jsonObjects);

            ArrayList<Map> maps1 = new ArrayList<>();
            JSONObject legend = (JSONObject) jsonObject.get("legend");
            for (int i = 1; i < xilieList.size(); i++) {
                Map<String, Object> map = new HashMap<>();
                map.put("name", xilieList.get(i));
                maps1.add(map);
            }

            legend.put("data", maps1);
            jsonObject.put("legend", legend);
            //求最大值&&最小值
            List<JSONObject> yAxis = (List<JSONObject>) jsonObject.get("yAxis");
            for (int i = 0; i < yAxis.size(); i++) {
                JSONObject jsonObject2 = yAxis.get(i);
                jsonObject2.put("max", minMax.max);
                jsonObject2.put("min", minMax.min);
            }
            jsonObject.put("yAxis", yAxis);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        } finally {
            workbook.close();
        }
    }*/

/*
    //按数据子集绘制直方图
    @PostMapping("/subsetHistogram/v2/importData")
    public AjaxResult v2subsetHistogram(@RequestBody List<Map<String, Object>> dataList, Long configId) throws Exception {
        //XSSFWorkbook workbook = null;
        try {
*//*            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);*//*

            Sheet sheet = ExcelUtils.convertDataListToSheetRemoveNineAndOne(dataList);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> groupName = new ArrayList<>();

            List<List<List<Object>>> newList = new ArrayList<>();

            List<List<Object>> list = new ArrayList<>();


            for (int i = 1; i < getdata.size(); i++) {
                List<String> sdata = getdata.get(i);
                List<Object> newList1 = new ArrayList<>();
                for (int i1 = 0; i1 < sdata.size(); i1++) {
                    String s = sdata.get(i1);
                    if (ObjectUtils.isNotEmpty(s) && !s.equals("")) {
                        if (i1 == 1) {
                            newList1.add(Double.parseDouble(s));
                        } else {
                            newList1.add(s);
                        }
                    }
                }


                if (i == getdata.size() - 1) {
                    list.add(newList1);
                    newList.add(list);
                } else if (i == 1) {
                    List<String> gName = getdata.get(i);
                    groupName.add(gName.get(0));
                } else {
                    if (getdata.get(i).size() == 1) {
                        List<String> gName = getdata.get(i);
                        groupName.add(gName.get(0));
                        newList.add(list);
                        list = new ArrayList<>();
                    } else {
                        list.add(newList1);
                    }
                }
            }

            ArrayList<Map> maps = new ArrayList<>();

            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());


            for (int i = 0; i < newList.size(); i++) {
                List<List<Object>> lists = newList.get(i);
                HashMap<String, Object> map = new HashMap<>();
                map.put("source", lists);
                maps.add(map);
            }

            List<JSONObject> jsonObjects = new ArrayList<>();
            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject jsonObject1 = series.get(0);
            for (int i = 0; i < groupName.size(); i++) {
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.putAll(jsonObject1);
                jsonObject2.put("name", groupName.get(i));
                jsonObject2.put("xAxisIndex", i);
                jsonObject2.put("yAxisIndex", i);
                jsonObject2.put("datasetIndex", i);

                jsonObjects.add(jsonObject2);
            }

            jsonObject.put("dataset", maps);
            jsonObject.put("series", jsonObjects);
            return AjaxResult.success(JSON.toJSONString(jsonObject));
        } finally {
         //   workbook.close();
        }
    }*/


    //多面多层圆滑折线图、成对多层圆滑折线图
    //multiSidAndLaySmooth/importData
    @PostMapping("/multiSidAndLaySmooth/v2/importData")
    public AjaxResult v2multiSidAndLaySmooth(@RequestBody List<Map<String, Object>> dataList, Long configId) throws Exception {

       // XSSFWorkbook workbook = null;
        try {
   /*         workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);*/
            Sheet sheet = ExcelUtils.convertDataListToSheetRemoveNineAndOne(dataList);
            //获取数据
            List<List<String>> excelData = ExcelDataUtils.getData(sheet);
            //数据判空
            if (excelData.size() <= 1) {
                return AjaxResult.error("请填充数据", null);
            }

            //这里就是表面分组
            List<Object> xilieList = new ArrayList<>();
            //获取分组
            List<String> xilis = excelData.get(0);
            for (int i = 0; i < xilis.size(); i++) {
                String s = xilis.get(i);
                if (ObjectUtils.isNotEmpty(s) && !s.equals("")) {
                    xilieList.add(s);
                }
            }

            //获取分组
            List<String> groupName = excelData.get(0).stream().skip(1).collect(Collectors.toList());
            excelData.remove(0);
            //封装返回数据
            //所有数据
            List<String> allData = excelData.stream().skip(1).flatMap(row -> row.stream().skip(1)).collect(Collectors.toList());
            //对数据进行字符串处理:获取最大最小值
            ExcelDataUtils.Result minMax = ExcelDataUtils.findMinMax(allData);

            //数据处理
            List<List<Object>> tempList = new ArrayList<>();
            List<List<List<Object>>> groupList = new ArrayList<>();
            int start = 0;
            int end = 0;
            for (int i = 0; i < excelData.size(); i++) {
                if (i < excelData.size() - 1 && excelData.get(i + 1).size() != 1) {
                    //0 - 24
                    //碰到end
                    end = i + 1;
                } else {
                    // i =25 end = 24
                    //遇到第一个size=1 将上一组数据归档
                    //开始操作
                    for (int j = start; j <= end; j++) {
                        List<Object> numbers = new ArrayList<>();
                        for (String s : excelData.get(j)) {
                            // 数据处理：如果是纯数字则转换为 Double，否则保持不变
                            if (s.matches("-?\\d+(\\.\\d+)?")) {
                                numbers.add(Double.parseDouble(s));
                            } else {
                                numbers.add(s);
                            }
                        }
                        tempList.add(numbers);
                    }
                    groupList.add(tempList);
                    tempList = new ArrayList<>();
                    start = end;
                }
            }
            //组装source
            List<List<List<Object>>> result = groupList.stream()
                    .skip(1) // 跳过第一个大集合
                    .map(innerList -> innerList.stream()
                            .skip(1) // 跳过每个内部集合的第一个元素
                            .collect(Collectors.toList()))
                    .collect(Collectors.toList());
            List<List<List<Object>>> groupData = new ArrayList<>();
            groupData.add(groupList.get(0));
            groupData.addAll(result);
            //source资源集合
            List<Map> maps = new ArrayList<>();

            for (List<List<Object>> item : groupData) {
                List<Object> title = item.get(0);
                title.addAll(groupName);
                item.remove(0);
                item.add(0, title);
                HashMap<String, Object> map = new HashMap<>();
                map.put("source", item);
                maps.add(map);
            }

            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            //组装json
            List<JSONObject> jsonObjects = new ArrayList<>();
            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject jsonObject1 = series.get(0);
            for (int i = 0; i < 2; i++) {
                for (int j = 1; j < xilieList.size(); j++) {
                    JSONObject jsonObject2 = new JSONObject();
                    jsonObject2.putAll(jsonObject1);
                    jsonObject2.put("name", xilieList.get(j));
                    jsonObject2.put("xAxisIndex", i);
                    jsonObject2.put("yAxisIndex", i);
                    jsonObject2.put("datasetIndex", i);
                    jsonObjects.add(jsonObject2);
                }
            }
            jsonObject.put("dataset", maps);
            jsonObject.put("series", jsonObjects);

            ArrayList<Map> maps1 = new ArrayList<>();
            JSONObject legend = (JSONObject) jsonObject.get("legend");
            for (int i = 1; i < xilieList.size(); i++) {
                Map<String, Object> map = new HashMap<>();
                map.put("name", xilieList.get(i));
                maps1.add(map);
            }

            legend.put("data", maps1);
            jsonObject.put("legend", legend);
            //求最大值&&最小值
            List<JSONObject> yAxis = (List<JSONObject>) jsonObject.get("yAxis");
            for (int i = 0; i < yAxis.size(); i++) {
                JSONObject jsonObject2 = yAxis.get(i);
                jsonObject2.put("max", minMax.max);
                jsonObject2.put("min", minMax.min);
            }
            jsonObject.put("yAxis", yAxis);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        } finally {
           // workbook.close();
        }
    }

/*
    //散点图矩阵
    @PostMapping("/scatterplotMatrix/importData")
    public AjaxResult scatterplotMatrix(MultipartFile file, Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> xilieList = getdata.get(0);
            getdata.remove(0);

            List<String> groupName = new ArrayList<>();

            List<List<List<String>>> newList = new ArrayList<>();

            List<List<String>> list = new ArrayList<>();


            for (int i = 0; i < getdata.size(); i++) {
                if (i == getdata.size() - 1) {
                    List<String> sdata = getdata.get(i);
                    list.add(sdata);
                    newList.add(list);
                } else if (i == 0) {
                    List<String> gName = getdata.get(i);
                    groupName.add(gName.get(0));
                } else {
                    if (getdata.get(i).size() == 1) {
                        List<String> gName = getdata.get(i);
                        groupName.add(gName.get(0));
                        newList.add(list);
                        list = new ArrayList<>();
                    } else {
                        List<String> sdata = getdata.get(i);
                        ArrayList<String> newList1 = new ArrayList<>();
                        for (int i1 = 0; i1 < sdata.size(); i1++) {
                            String s = sdata.get(i1);
                            if (ObjectUtils.isNotEmpty(s) && !s.equals("")) {
                                newList1.add(s);
                            }
                        }
                    }
                }
            }

            ArrayList<Map> maps = new ArrayList<>();

            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());


            for (int i = 0; i < newList.size(); i++) {
                List<List<String>> lists = newList.get(i);
                lists.add(0, xilieList);
                HashMap<String, Object> map = new HashMap<>();
                map.put("source", lists);
                maps.add(map);
            }

            List<JSONObject> jsonObjects = new ArrayList<>();
            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");

            for (int i = 0; i < series.size(); i++) {
                JSONObject jsonObject2 = series.get(i);
                for (int i1 = 1; i1 < xilieList.size(); i1++) {

                    jsonObject2.put("name", xilieList.get(i1));

                    jsonObjects.add(jsonObject2);
                }
            }

            jsonObject.put("dataset", maps);
            jsonObject.put("series", jsonObjects);
            return AjaxResult.success(JSON.toJSONString(jsonObject));
        } finally {
            workbook.close();
        }
    }*/
    //散点图矩阵
    @PostMapping("/scatterplotMatrix/v2/importData")
    public AjaxResult v2scatterplotMatrix(@RequestBody List<Map<String, Object>> dataList, Long configId) throws Exception {
       // XSSFWorkbook workbook = null;
        try {
 /*           workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);*/
            Sheet sheet = ExcelUtils.convertDataListToSheetRemoveNineAndOne(dataList);

            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> xilieList = getdata.get(0);
            getdata.remove(0);

            List<String> groupName = new ArrayList<>();

            List<List<List<String>>> newList = new ArrayList<>();

            List<List<String>> list = new ArrayList<>();


            for (int i = 0; i < getdata.size(); i++) {
                if (i == getdata.size() - 1) {
                    List<String> sdata = getdata.get(i);
                    list.add(sdata);
                    newList.add(list);
                } else if (i == 0) {
                    List<String> gName = getdata.get(i);
                    groupName.add(gName.get(0));
                } else {
                    if (getdata.get(i).size() == 1) {
                        List<String> gName = getdata.get(i);
                        groupName.add(gName.get(0));
                        newList.add(list);
                        list = new ArrayList<>();
                    } else {
                        List<String> sdata = getdata.get(i);
                        ArrayList<String> newList1 = new ArrayList<>();
                        for (int i1 = 0; i1 < sdata.size(); i1++) {
                            String s = sdata.get(i1);
                            if (ObjectUtils.isNotEmpty(s) && !s.equals("")) {
                                newList1.add(s);
                            }
                        }
                    }
                }
            }

            ArrayList<Map> maps = new ArrayList<>();

            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());


            for (int i = 0; i < newList.size(); i++) {
                List<List<String>> lists = newList.get(i);
                lists.add(0, xilieList);
                HashMap<String, Object> map = new HashMap<>();
                map.put("source", lists);
                maps.add(map);
            }

            List<JSONObject> jsonObjects = new ArrayList<>();
            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");

            for (int i = 0; i < series.size(); i++) {
                JSONObject jsonObject2 = series.get(i);
                for (int i1 = 1; i1 < xilieList.size(); i1++) {

                    jsonObject2.put("name", xilieList.get(i1));

                    jsonObjects.add(jsonObject2);
                }
            }

            jsonObject.put("dataset", maps);
            jsonObject.put("series", jsonObjects);
            return AjaxResult.success(JSON.toJSONString(jsonObject));
        } finally {
           // workbook.close();
        }
    }
}
